using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using TaskManager.Common;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using Tools;


namespace TaskManager.WebTask
{

    public class ManageKindLink 
    {
        private string TARGET= @"http://study.360bao.com/list/all_health/{0}.html";
        private int PAGECOUNT = 10;

        public void AllKindLink()
        {
            for (int i = 1; i <= PAGECOUNT; i++)
            {
                string aLink = String.Format(TARGET,i.ToString());
                SingleKindLink(aLink);
            }
        
        }


        private void SingleKindLink(string TargetUrl)
        {

            Encoding _PageEncode = Encoding.GetEncoding(@"gb2312");
            Stream webContent = Tools.WebUtility.GetStreamFromUrl(TargetUrl, TaskConfig.MAXTIMEOUT);
            StreamReader sr = new StreamReader(webContent, _PageEncode);
            string _PageContent = sr.ReadToEnd();
            _PageContent = _PageContent.Replace("\"", "");
            _PageContent = _PageContent.Replace("\'", "");


            if (_PageContent!="")
            {
                List<string> lLinkList = new List<string>();
                //RegexOptions option = (RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
                string regex = @"<title>(?<Title>.*)</title>";
                string field = @"Title";
                lLinkList = RegTools.GetSubList(_PageContent, regex, field);

                //SaveKindLinkListToFile(lLinkList);
 
            }
 
            
        }

        private void SaveKindLinkListToFile(List<string> lKindLinkList)
        {

            string FileName = Application.StartupPath + "\\Task\\KindLinkList.txt";
            FileStream myStream = File.Open(FileName, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            try
            {
                foreach (string sLink in lKindLinkList)
                {
                    sw.WriteLine(sLink);
                }
            }
            finally
            {
                sw.Close();
                myStream.Close();            
            }

       
        }


 
    }
}
